package cc.fs.kotlinproject.utlis

import android.app.Dialog
import android.app.DialogFragment
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AlertDialog
import cc.fs.kotlinproject.R
import org.jetbrains.anko.AlertDialogBuilder

/**
 * Created by fostion on 2015/8/14.
 */

class AlertDialogHelper : DialogFragment() {

    companion object {
        public fun newInstance(title: Int): AlertDialogHelper {
            val dialog = AlertDialogHelper()
            val bundle = Bundle()
            bundle.putInt("title", title)
            dialog.setArguments(bundle)
            return dialog
        }
    }

    public var sureCallback: (() -> Unit)? = null;
    public var cancleCallback: (() -> Unit)? = null;

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog? {

        val title = getArguments().get("title")

        return AlertDialog.Builder(getActivity())
                .setIcon(R.mipmap.ic_launcher)
                .title(title)
                .message(title)
                .setCancelable(false)
                .setPositiveButton("sure", object : DialogInterface.OnClickListener {
                    override fun onClick(dialog: DialogInterface, which: Int) {
                        sureCallback?.invoke()
                    }
                })
                .setNegativeButton("cancle", object : DialogInterface.OnClickListener {
                    override fun onClick(dialog: DialogInterface, which: Int) {
                        cancleCallback?.invoke()
                    }
                }).create()
    }

    fun AlertDialog.Builder.title(title: Any): AlertDialog.Builder {
        when (title) {
            is Int -> return setTitle(title as Int)
            else -> return setTitle(title as String)
        }
    }

    fun AlertDialog.Builder.message(content: Any): AlertDialog.Builder {
        when (content) {
            is Int -> return setMessage(content as Int)
            else -> return setMessage(content as String)
        }
    }

    fun Bundle.putAny(key: String, value: Any) {
        when (value) {
            is Int -> putInt(key, value as Int)
            else -> putString(key, value as String)
        }
    }


}
